package com.weed.table.head;

import com.weed.table.Column.TableColumnEx;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import java.util.Vector;
import javax.swing.table.TableColumn;
import java.util.Enumeration;
import java.awt.Component;
import java.awt.event.MouseEvent;
import javax.swing.table.TableCellEditor;
import java.util.EventObject;
import javax.swing.event.*;

import javax.swing.JTable;
import javax.swing.JComponent;
import java.awt.Rectangle;


/**
 * <p>Title: <</p>
 * <p>Description: 组表头。</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Jason
 * @version 1.0
 * |-----------------------------------------------------|
 * |        |       Name      |         Language         |
 * |        |-----------------|--------------------------|
 * |  SNo.  |        |        |        |      Others     |
 * |        |   1    |    2   | Native |-----------------|
 * |        |        |        |        |   2    |   3    |
 * |-----------------------------------------------------|
 * |        |        |        |        |        |        |
 * @version 1.0 2003/09/7
 * @author Jason zhuo
 */



public class JTableHeaderEx extends JTableHeader implements CellEditorListener{

  private static final String uiClassID = "JTableHeaderExUI";
  protected Vector columnGroups = null;
  public final int HEADER_ROW = -10;
  private int editingColumn;
  private TableCellEditor cellEditor;
  private Component editorComp;

  /**
   * 构造函数,列模式。
   * @param columnModel
   */
  public JTableHeaderEx(TableColumnModel columnModel) {
   super(columnModel);
   setReorderingAllowed(false);
   cellEditor = null;
   super.setUI(new JTableHeaderExUI());
   recreateTableColumn(columnModel);
 }

  /**
   * 添加列组.
   * @param g
   */
  public void addColumnGroup(ColumnGroup g) {
   if (columnGroups == null) {
     columnGroups = new Vector();
   }
   columnGroups.addElement(g);
 }
 /**
  * 获得列组数量
  */
 public int getColumnGroupSize(){
   if(columnGroups!=null)
     return columnGroups.size();
   return 0;
 }
 /**
  * 获得列组列表.
  */
 public Vector getColumnGroups(){
   return columnGroups;
 }
/**
 * 获得指定列所属的列组。
 * @param col
 * @return
 */
 public Enumeration getColumnGroups(TableColumn col) {
    if (columnGroups == null) return null;
    Enumeration enum1 = columnGroups.elements();
    while (enum1.hasMoreElements()) {
      ColumnGroup cGroup = (ColumnGroup)enum1.nextElement();
      Vector v_ret = (Vector)cGroup.getColumnGroups(col,new Vector());
      if (v_ret != null) {
        return v_ret.elements();
      }
    }
    return null;
  }
  /**
   * 设置列间。
   */
  public void setColumnMargin() {
    if (columnGroups == null) return;
    int columnMargin = getColumnModel().getColumnMargin();
    Enumeration enum1 = columnGroups.elements();
    while (enum1.hasMoreElements()) {
      ColumnGroup cGroup = (ColumnGroup)enum1.nextElement();
      cGroup.setColumnMargin(columnMargin);
    }
  }
/******************2003.09.08************************/

 public void updateUI(){
   setUI(new JTableHeaderExUI());
   resizeAndRepaint();
   invalidate();
 }

 protected void recreateTableColumn(TableColumnModel columnModel) {
   int n = columnModel.getColumnCount();
   TableColumnEx[] newCols = new TableColumnEx[n];
   TableColumn[] oldCols = new TableColumn[n];
   for (int i=0;i<n;i++) {
     oldCols[i] = columnModel.getColumn(i);
     newCols[i] = new TableColumnEx();
     newCols[i].copyValues(oldCols[i]);
   }
   for (int i=0;i<n;i++) {
     columnModel.removeColumn(oldCols[i]);
   }
   for (int i=0;i<n;i++) {
     columnModel.addColumn(newCols[i]);
   }
 }

 public boolean editCellAt(int index){
   return editCellAt(index);
 }

 public boolean editCellAt(int index, EventObject e){
   if (cellEditor != null && !cellEditor.stopCellEditing()) {
     return false;
   }
   if (!isCellEditable(index)) {
     return false;
   }
   TableCellEditor editor = getCellEditor(index);

   if (editor != null && editor.isCellEditable(e)) {
     editorComp = prepareEditor(editor, index);
     editorComp.setBounds(getHeaderRect(index));
     add(editorComp);
     editorComp.validate();
     setCellEditor(editor);
     setEditingColumn(index);
     editor.addCellEditorListener(this);

     return true;
   }
   return false;
 }


 public boolean isCellEditable(int index) {
   if (getReorderingAllowed()) {
     return false;
   }
   int columnIndex = columnModel.getColumn(index).getModelIndex();
   TableColumnEx col = (TableColumnEx)columnModel.getColumn(columnIndex);
   return col.isHeaderEditable();
 }

 public TableCellEditor getCellEditor(int index) {
   int columnIndex = columnModel.getColumn(index).getModelIndex();
   TableColumnEx col = (TableColumnEx)columnModel.getColumn(columnIndex);
   return col.getHeaderEditor();
 }

 public void setCellEditor(TableCellEditor newEditor) {
   TableCellEditor oldEditor = cellEditor;
   cellEditor = newEditor;

   // firePropertyChange

   if (oldEditor != null && oldEditor instanceof TableCellEditor) {
     ((TableCellEditor)oldEditor).removeCellEditorListener((CellEditorListener)this);
   }
   if (newEditor != null && newEditor instanceof TableCellEditor) {
     ((TableCellEditor)newEditor).addCellEditorListener((CellEditorListener)this);
   }
 }

 public Component prepareEditor(TableCellEditor editor, int index) {
   Object       value = columnModel.getColumn(index).getHeaderValue();
   boolean isSelected = true;
   int            row = HEADER_ROW;
   JTable       table = getTable();
   Component comp = editor.getTableCellEditorComponent(table,
                      value, isSelected, row, index);
   if (comp instanceof JComponent) {
           ((JComponent)comp).setNextFocusableComponent(this);
   }
   return comp;
 }

 public TableCellEditor getCellEditor() {
   return cellEditor;
 }

 public Component getEditorComponent() {
   return editorComp;
 }

 public void setEditingColumn(int aColumn) {
   editingColumn = aColumn;
 }

 public int getEditingColumn() {
   return editingColumn;
 }

 public void removeEditor() {
   TableCellEditor editor = getCellEditor();
   if (editor != null) {
     editor.removeCellEditorListener(this);

     requestFocus();
     remove(editorComp);

     int index = getEditingColumn();
     Rectangle cellRect = getHeaderRect(index);

     setCellEditor(null);
     setEditingColumn(-1);
     editorComp = null;

     repaint(cellRect);
   }
 }

 public boolean isEditing() {
   return (cellEditor == null)? false : true;
 }


 //
 // CellEditorListener
 //
 public void editingStopped(ChangeEvent e) {
   TableCellEditor editor = getCellEditor();
   if (editor != null) {
     Object value = editor.getCellEditorValue();
     int index = getEditingColumn();
     columnModel.getColumn(index).setHeaderValue(value);
     removeEditor();
   }
 }

 public void editingCanceled(ChangeEvent e) {
   removeEditor();
 }

}
